package renamer;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public class FileChooser extends JPanel implements ActionListener {

	private static final long serialVersionUID = 1L;
	JButton openButton;
	JFileChooser fileSelector;
	MusicTidy mt = new MusicTidy();

	public FileChooser() {
		super(new BorderLayout());
		fileSelector = new JFileChooser();
		fileSelector.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		openButton = new JButton("Select a directory");
		openButton.addActionListener(this);
		JPanel buttonPanel = new JPanel();
		buttonPanel.add(openButton);
		add(buttonPanel, BorderLayout.PAGE_START);
	}

	public void actionPerformed(ActionEvent click) {
		if (click.getSource() == openButton) {
			int returnVal = fileSelector.showOpenDialog(FileChooser.this);
			if (returnVal == JFileChooser.APPROVE_OPTION) {
				File dir = fileSelector.getSelectedFile();
				try {
					mt.execute(dir);
				} catch (Exception e2) {
					e2.printStackTrace();
				}
			}
		}
	}

	private static void createAndShowGUI() {
		JFrame frame = new JFrame("MusicRenamerDirectoryChooser");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setSize(300, 200);
		Toolkit toolkit = frame.getToolkit();
		Dimension size = toolkit.getScreenSize();
		frame.setLocation(size.width / 2 - frame.getWidth() / 2, size.height
				/ 2 - frame.getHeight() / 2);
		frame.add(new FileChooser());
		frame.pack();
		frame.setVisible(true);
	}

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				UIManager.put("swing.boldMetal", Boolean.FALSE);
				createAndShowGUI();
			}
		});
	}
}
